//
//  BIAdvertisementView.m
//  BIProjectModule
//
//  Created by yown on 2022/2/10.
//

#import "BIAdvertisementView.h"
#import "BIProjectContext.h"

#import <BIToolModule/BIProjectDefine.h>
#import <BIRouteModule/BIRouteManager.h>

#import <BIToolModule/NSString+Helper.h>
//#import <BIToolModule/NSTimer+Helper.h>

#import <Masonry/Masonry.h>
#import <SDWebImage/SDWebImage.h>

@interface BIAdvertisementView ()

@property (nonatomic) BIAdvertisementModel *model;
@property (nonatomic) NSTimer *timer;
@property (nonatomic, assign) NSUInteger time;

@property (nonatomic) UIImageView *adImageView;
@property (nonatomic) UIButton *enterBtn;

@end

@implementation BIAdvertisementView

+ (void)showWithModel:(BIAdvertisementModel *)model {
    UIWindow *keyWindow = [BIProjectContext sharedInstance].appDelegate.window;
    BIAdvertisementView *view = [[BIAdvertisementView alloc] initWithFrame:keyWindow.bounds];
    view.model = model;
    [view bi_setupView];
    [view bi_setupLayout];
    [view bi_setupTimer];
    [keyWindow addSubview:view];
}

#pragma mark - Setup

- (void)bi_setupView {
    // 广告图
    self.adImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"VersionUpgradeBg"]];
    self.adImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.adImageView.userInteractionEnabled = YES;
    [self.adImageView sd_setImageWithURL:[NSURL URLWithString:self.model.showAdvertisementModel.image] placeholderImage:[UIImage imageNamed:@"launch_image@3x.png"]];
    [self.adImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bi_clickAD)]];
    [self addSubview:self.adImageView];
  
    // 关闭按钮
    self.enterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.enterBtn.clipsToBounds = YES;
    self.enterBtn.layer.cornerRadius = 13;
    self.enterBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    self.enterBtn.adjustsImageWhenHighlighted = NO;
    [self.enterBtn setTitle:[NSString stringWithFormat:@"跳过 %@ ", self.model.showTime] forState:UIControlStateNormal];
    [self.enterBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.enterBtn addTarget:self action:@selector(bi_clickEnterButton) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.enterBtn];
}

- (void)bi_setupLayout {
    // 背景
    [self.adImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    // 关闭按钮
    [self.enterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@(96));
        make.height.mas_equalTo(@(30));
        make.right.mas_equalTo(self.adImageView.mas_right).offset(-20);
        make.top.mas_equalTo(self.adImageView.mas_top).offset(BIStatusBarHeight + 5);
    }];
}

- (void)bi_setupTimer {
    self.time = [self.model.showTime integerValue];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        [self bi_timeUpdate];
    }];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}

#pragma mark - Action

- (void)bi_timeUpdate {
    self.time--;
    if (self.time > 0) {
        [self.enterBtn setTitle:[NSString stringWithFormat:@"跳过 %@ ", @(self.time)] forState:UIControlStateNormal];
    } else {
        [self bi_clickEnterButton];
    }
}

- (void)bi_clickAD {
    [self bi_clickEnterButton];
    if (![NSString isEmpty:self.model.showAdvertisementModel.link]) {
        [[BIRouteManager sharedInstance] openVCURL:self.model.showAdvertisementModel.link isPresent:NO arguments:nil];
    }
}

- (void)bi_clickEnterButton {
    [self.timer invalidate];
    self.timer = nil;
    
    [UIView animateWithDuration:0.7 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


@end
